﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace UILibrary
{
    public partial class AboutBox : Form
    {
        public AboutBox(Assembly assembly)
        {
            InitializeComponent();

            this.Text = String.Format("About {0}", AssemblyTitle(assembly));
            this.labelProductName.Text = AssemblyProduct(assembly);
            this.labelVersion.Text = String.Format("Version {0}", AssemblyVersion(assembly));
            this.labelCopyright.Text = AssemblyCopyright(assembly);
            this.labelCompanyName.Text = AssemblyCompany(assembly);
        }

        #region Assembly Attribute Accessors

        public string AssemblyTitle(Assembly assmbl)
        {
            object[] attributes = assmbl.GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
            if (attributes.Length > 0)
            {
                AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                if (titleAttribute.Title != "")
                {
                    return titleAttribute.Title;
                }
            }
            return System.IO.Path.GetFileNameWithoutExtension(assmbl.CodeBase);
        }

        public string AssemblyVersion(Assembly assmbl)
        {
            return assmbl.GetName().Version.ToString();
        }

        public string AssemblyDescription(Assembly assmbl)
        {
            object[] attributes = assmbl.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
            if (attributes.Length == 0)
            {
                return "";
            }
            return ((AssemblyDescriptionAttribute)attributes[0]).Description;
        }

        public string AssemblyProduct(Assembly assmbl)
        {
            object[] attributes = assmbl.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
            if (attributes.Length == 0)
            {
                return "";
            }
            return ((AssemblyProductAttribute)attributes[0]).Product;
        }

        public string AssemblyCopyright(Assembly assmbl)
        {
            object[] attributes = assmbl.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
            if (attributes.Length == 0)
            {
                return "";
            }
            return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
        }

        public string AssemblyCompany(Assembly assmbl)
        {
            object[] attributes = assmbl.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            if (attributes.Length == 0)
            {
                return "";
            }
            return ((AssemblyCompanyAttribute)attributes[0]).Company;
        }
        #endregion
    }
}
